#pragma once
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <linux/stddef.h>
#include <RgaUtils.h>
#include <im2d.hpp>
#include "dma_alloc.h"

#include "mpp/rk_struct.h"
#include "base_puller.h"

namespace stream {

class RKStreamPullerNET:public BasePuller
{
public:
  RKStreamPullerNET(StreamPullerInfo* spinfo, LogInfo* log_info);
  ~RKStreamPullerNET();

public:
  int init_params();
  int start_pull();
  int stop_pull();
  int get_frame(stream::ImageBlob& buffer);

private:
  int init_mpp();
  int init_rga();
  int init_resize_rga();
  int release_mpp(int ret);
  int release_rga(int ret);
  int release_resize_rga(int ret);
  void release_ffmpeg();
  int copy_buffer(MppFrame &frame,cv::Mat& rgbImg,std::vector<char>& out_buf);

public:


private:

  AVFormatContext *ifmt_ctx = nullptr;
  AVDictionary *optionsDict = nullptr;
  AVStream *pVst = nullptr;
  AVCodecContext *pVideoCodecCtx = nullptr;
  AVCodec *pVideoCodec = nullptr;
  AVFrame *pFrame = nullptr;
  int video_st_index = -1;
  int audio_st_index = -1;
  float cur_fps;
  
  std::mutex queue_lock;
  std::queue<stream::ImageBlob> queue_data;

  //rk
  MppCtx ctx;
	MppApi* mpi;
	MppBufferGroup frm_grp;
	MppBufferGroup pkt_grp;
	MppParam param;
	// RK_U32 need_split;
	MpiCmd mpi_cmd;
	size_t max_usage;


  // rga
  int src_format, dst_format, resize_format;
  int src_buf_size, dst_buf_size, resize_buf_size;
  int src_dma_fd, dst_dma_fd, resize_dma_fd;
  char* src_buf;
  char* dst_buf;
  char* resize_buf;
  rga_buffer_t src_img, dst_img, resize_img;
  rga_buffer_handle_t src_handle, dst_handle, resize_handle;

  // init module
  // void *display = nullptr;
  // void *vps = nullptr;
  int tmp_cnt;
  int total_yuv2rgb_time = 0;
  int total_yuv2rgb_cnt = 0;

  std::string rtsp_main;
  

};


}   // namespace camera
